package com.database.daos.imp;

import javax.sql.DataSource;

import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import com.database.daos.ISumDao;
import com.database.objects.*;

public class SumDao extends GenericDao<Double> implements ISumDao {

	private static final long serialVersionUID = 8977473437743447366L;

	private SimpleJdbcTemplate jdbcTemplate;

	@Override
	public Double getSumNumberContributionsByProjectAndTypeAndStatus(Project project, Type type, Status status, String start, String finish) {
		String sql = "select sum(con_number) from contribution con inner join developer dev on dev.dev_id = con.dev_id where dev.pro_name = ? and con_type = ? and con_status = ? and con_date >= ? and con_date <= ?";
		Long value = jdbcTemplate.queryForLong(sql, project.getName(), type.toString(), status.toString(), start, finish);
		return value != null ? Double.valueOf(value.toString()) : 0.0;
	}

	@Override
	public Double getSumNumberContributionsByDeveloperAndTypeAndStatus(Developer developer, Type type, Status status, String start, String finish) {
		String sql = "select sum(con_number) from contribution con where dev_id = ? and con_type = ? and con_status = ? and con_date >= ? and con_date <= ?";
		Long value = jdbcTemplate.queryForLong(sql, developer.getId(), type.toString(), status.toString(), start, finish);
		return value != null ? value : 0.0;
	}

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}

}